<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * @ID model_hotel.php
 * 酒店信息 model
 * @date 2013-1-30 
 * @author zhaojianjun zjj008@gmail.com
 * @copyright zhuna Inc , all rights reserved
 * 
 */

class Model_hotel extends CI_Model
{
    private $cachePath;
    
    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('model_keywords'); 
        $this->load->model('model_sysconfig');
        $this->load->library('tool');
        
        $this->cachePath = ROOTPATH.DIRECTORY_SEPARATOR.$this->config->config['cache_path'];//缓存存放目录
    }
    
    /**
     * 获取酒店详细信息
     * @param <int> $hotelId
     * return <array> $info
     */
    function getHotelInfo($hotelId)
    {
        $apiUrl = "http://union.api.zhuna.cn/hotel/json/hotel_$hotelId.json";
        $cacheName = "hotel_hid{$hotelId}.json";
        $api_list = $this->tool->create_cache($apiUrl,$this->cachePath."hotel/hotel/",$cacheName);
        return $list = json_decode($api_list,true);  
    }  
    
    /**
     * 获取酒店评论
     * @param <int> $hotelId
     * @param <int> $nums
     * return <array> $list
     */
    public function getCommentList($hotelId = '',$nums = 5, $pagesize = 10)
    {
        $apiUrl = CFG_INTERFACE_API."hotel.comment&hid=$hotelId&pagesize=$pagesize";
        $cacheName = "hotel_comment_hid{$hotelId}.json";
        $api_list = $this->tool->create_cache($apiUrl,$this->cachePath."comment/comment/",$cacheName);
        $list = json_decode($api_list,true);
        //截取相应的返回数目，处理数据方便显示
    	foreach ($list['reqdata'] as $key=>$val) {
    		$yingxiang_show = $this->tool->tags_ary($val['yinxiang']);
    		$list['reqdata'][$key]['yinxiang_show'] = array_slice($yingxiang_show,0,4);
    		$renqun = explode(',',$val['renqun']);
    		$list['reqdata'][$key]['comment_pic_text'] = $renqun[0];
    		switch($renqun[0]){
    			case '其他':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_01.gif';
    				break;
    			case '携带儿童的家庭 ':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_02.gif';
    				break;
    			case '商务旅客':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_03.gif';
    				break;
    			case '单独旅行者':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_04.gif';
    				break;
    			case '户外探险旅行者':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_05.gif';
    				break;
    			case '夫妇情侣':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_06.gif';
    				break;
    			case '与朋友同行':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_07.gif';
    				break;
    			case '与客户同行':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_08.gif';
    				break;
    			case '家族旅游':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_09.gif';
    				break;
    			case '带宠物出行':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_10.gif';
    				break;
    			default :
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_01.gif';
    				$list['reqdata'][$key]['comment_pic_text'] = '其他';
    		}
    	}
    	$list['reqdata'] = array_slice($list['reqdata'],0,$nums);
    	return $list;
    }
    
    /**
     * 根据城市id获取酒店评论
     * @param <int> $cityid
     * @param <int> $nums
     * return <array> $list
     */
    public function getCommentListByCiytid($cityid = '0101',$nums = 5, $pagesize = 10, $page = 1)
    {
    	$apiUrl = CFG_INTERFACE_API."comment.list&cityid=$cityid&pagesize=$pagesize&page=$page";
    	//缓存前几页
    	if ($page<5) {
    		$cacheName = "hotel_comment_cid{$cityid}_page{$page}.json";
    		$api_list = $this->tool->create_cache($apiUrl,$this->cachePath."comment/comment/",$cacheName);
    	} else {
    		$api_list = file_get_contents($apiUrl);
    	}
    	$list = json_decode($api_list,true);
    	//截取相应的返回数目，处理数据方便显示
    	foreach ($list['reqdata'] as $key=>$val) {
    		$list['reqdata'][$key]['date_show'] = date("y/m/d",$val['time']);
    		$yingxiang_show = $this->tool->tags_ary($val['yinxiang']);
    		$list['reqdata'][$key]['yinxiang_show'] = array_slice($yingxiang_show,0,4);
    		$renqun = explode(',',$val['renqun']);
    		$list['reqdata'][$key]['comment_pic_text'] = $renqun[0];
    		switch($renqun[0]){
    			case '其他':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_01.gif';
    				break;
    			case '携带儿童的家庭 ':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_02.gif';
    				break;
    			case '商务旅客':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_03.gif';
    				break;
    			case '单独旅行者':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_04.gif';
    				break;
    			case '户外探险旅行者':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_05.gif';
    				break;
    			case '夫妇情侣':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_06.gif';
    				break;
    			case '与朋友同行':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_07.gif';
    				break;
    			case '与客户同行':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_08.gif';
    				break;
    			case '家族旅游':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_09.gif';
    				break;
    			case '带宠物':
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_10.gif';
    				break;
    			default :
    				$list['reqdata'][$key]['comment_pic'] = 'dianping_01.gif';
    				$list['reqdata'][$key]['comment_pic_text'] = '其他';
    		}
    	}
    	$list['reqdata'] = array_slice($list['reqdata'],0,$nums);
    	return $list;
    }
    
    /**
     * 获取酒店问答
     * @param <int> $hotelId 
     * @param <int> $nums  
     * @return array $list
     */
    public function getQuestionList($hotelId = '',$nums = 5, $pagesize = 10)
    {
    	$apiUrl = CFG_INTERFACE_API."hotel.question&hid=$hotelId&pagesize=$pagesize";
        $cacheName = "hotel_question_hid{$hotelId}.json";
        $api_list = $this->tool->create_cache($apiUrl,$this->cachePath."comment/question/",$cacheName);
        $list = json_decode($api_list,true);
        //截取相应的返回数目，处理数据方便显示
        foreach($list['reqdata'] as $key=>$val){
            $val = explode('{un}',$val['content']);
            $list['reqdata'][$key]['question'] = $val['0'];  
            $list['reqdata'][$key]['answer'] = $val['1'];                            
        }
        $questionList = array_slice($list['reqdata'],0,$nums);
        $list['reqdata'] = $this->tool->change_array($questionList);
        
        return $list;
    }
    
    /**
     * 根据城市id获取获取酒店问答
     * @param <int> $cityid 
     * @param <int> $nums  
     * @return array $list
     */
    public function getQuestionListByCityid($cityid = '', $num= 5, $pagesize = 10, $page = 1)
    {
    	$apiUrl = CFG_INTERFACE_API."question.list&cityid=$cityid&pagesize=$pagesize&page=$page";
    	//缓存前几页
    	if ($page<5) {
    		$cacheName = "hotel_question_cid{$cityid}_page{$page}.json";
    		$api_list = $this->tool->create_cache($apiUrl,$this->cachePath."comment/question/",$cacheName);
    	} else {
    		$api_list = file_get_contents($apiUrl);
    	}
    	$list = json_decode($api_list,true);
    	//截取相应的返回数目，处理数据方便显示
    	$questionList = array_slice($list['reqdata'],0,$num);
    	$list['reqdata'] = $this->tool->change_array($questionList);
    	
    	return $list;
    }
    
    /**
     * 酒店交通指南组合成数组
     * @param <mixed> $traffic
     * @return <array> $result
     */
    public function trafficShow($traffic)
    {
        $traffic_array = explode('-', $traffic);
        $title = $traffic_array[0];
        unset($traffic_array[0]);
        $content = $traffic_array; 
        return $traffic_array = array('title'=>$title,'content'=>$content);
        
    }
    
    /**
     * 得到周边酒店：根据经纬度
     * @param <mixed> $lat,$lng,$limit
     * @return <array> $welcome_hotel 
     */
    public function getRoundHotel($lat,$lng,$limit)
    {	
    	$field = "ID,HotelName,min_jiage,juli,Picture";
        $welcome_hotel_api = file_get_contents(CFG_INTERFACE_API."hotel.search.nearby&lat=$lat&lng=$lng&pagesize=$limit&field=$field");
        return $welcome_hotel = json_decode($welcome_hotel_api,true);      
    }
    
    /**
     * 得到周边酒店：根据xy
     * @param <mixed> $lat,$lng,$limit
     * @return <array> $welcome_hotel
     */
    public function getRoundHotelByXY($x,$y,$limit)
    {
    	$field = "ID,HotelName,min_jiage,juli,Picture";
    	$welcome_hotel_api = file_get_contents(CFG_INTERFACE_API."search&x=$x&y=$y&pagesize=$limit&field=$field");
    	return $welcome_hotel = json_decode($welcome_hotel_api,true);
    }
    
    /**
     * 得到附近地标
     * @param <mixed> 
     * @return <array> 
     */
    public function getLableByHotelXY($cityid,$lat,$lng)
    {
    	$api = file_get_contents(CFG_INTERFACE_API."lable.search.nearby&cityid=$cityid&lat=$lat&lng=$lng");
    	$lableListJson = json_decode($api,true);
    	return $lableList = $lableListJson['reqdata'];
    }
}

?>